package com.api.smoke.department;

import java.util.Collection;
import java.util.Iterator;

import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.api.test.VoidContextLoader;
import com.api.util.AssertUtil;
import com.api.util.DataUtil;
import com.api.util.GenericUtils.GenericType;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.impl.Domain;
import com.api.util.repo.impl.DomainUser;
import com.api.util.repo.impl.ResultFileDto;
import com.api.util.repo.impl.ZyncroRepo;
import com.util.zyncroweb.DomainProperty;
import com.util.zyncroweb.EnterpriseProperty;
import com.util.zyncroweb.ZyncroWebApi;
import com.zyncro.api.IDepartmentApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.DepartmentApi;
import com.zyncro.api.model.DepartmentFilterApiType;
import com.zyncro.api.model.GroupOrderFieldFilterApiType;
import com.zyncro.api.model.OrderFilterApiType;
import com.zyncro.api.model.PagedItemsResultApi;

@ContextConfiguration(loader = VoidContextLoader.class)
public class GetDepartmentsSmoke extends AbstractApiTest {

	private PagedItemsResultApi<DepartmentApi> expectedResults;

	@BeforeClass
	protected void setUp() throws Exception {

		final String domainName = this.getClass().getSimpleName();
		final ZyncroRepo zyncroRepo = getRepo();
		final DomainUser mainAdmin = zyncroRepo.getMainUser(domainName);

		getApi().login(mainAdmin.getAppId(), mainAdmin.getPassword());

		ResultFileDto file = zyncroRepo.getPropertyDto(domainName, new DtoFactory<ResultFileDto>() {

			public ResultFileDto createDto() {

				ZyncroWebApi zyncroWebApi = getZyncroWebApi();
				String env = System.getProperty("env");

				Domain domain = zyncroRepo.getDomain(domainName);
				zyncroWebApi.login(mainAdmin.getAppId(), mainAdmin.getPassword());

				getZyncroWebApi().editDomainProperty(domain.getDomainUrn(), DomainProperty.ENTERPRISES, EnterpriseProperty.DEPARTMENT);

				ResultFileDto resultFile = new ResultFileDto();
				resultFile.setFileName(String.format("./json/%s/%s.json", env, domainName));

				try {
					getApi().getService(IDepartmentApiService.class).createDepartment("nombre", "description", "address", "telephone",
							"email", "web", "fax", true, null, null, false);

					getApi().getService(IDepartmentApiService.class).createDepartment("nombre1", "description", "address", "telephone",
							"email", "web", "fax", true, null, null, false);

					getApi().getService(IDepartmentApiService.class).createDepartment("nombre2", "description", "address", "telephone",
							"email", "web", "fax", true, null, null, false);

					getApi().getService(IDepartmentApiService.class).createDepartment("nombr3", "description", "address", "telephone",
							"email", "web", "fax", true, null, null, false);

					getApi().getService(IDepartmentApiService.class).createDepartment("nombre4", "description", "address", "telephone",
							"email", "web", "fax", true, null, null, false);

					PagedItemsResultApi<DepartmentApi> result = getApi().getService(IDepartmentApiService.class).getDepartments(0, 0, null,
							null, null, null);

					DataUtil.saveDtoToFile(result, resultFile.getFileName());

				} catch (Exception e) {

					throw new IllegalStateException("Exception creating GetDepartments test data", e);
				}

				return resultFile;
			}
		});

		this.expectedResults = DataUtil.getDtoFromFile(file.getFileName(), new GenericType<PagedItemsResultApi<DepartmentApi>>() {
		});
	}

	@Test
	public void testGetDepartments() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 2;
		DepartmentFilterApiType departmentFilter = null;
		String startsWith = null;

		GroupOrderFieldFilterApiType orderFilerField = null;
		OrderFilterApiType orderFilter = null;

		PagedItemsResultApi<DepartmentApi> result = getApi().getService(IDepartmentApiService.class).getDepartments(pageNumber,
				itemsPerPage, departmentFilter, startsWith, orderFilerField, orderFilter);

		Assert.assertEquals(result.getElements().size(), itemsPerPage, "itemsPerPage");
		Assert.assertEquals(result.getPageNumber(), 1, "pageNumber");
		Assert.assertEquals(result.getTotalItems(), expectedResults.getTotalItems());

		Collection<DepartmentApi> resultElements = result.getElements();
		Collection<DepartmentApi> expectedElements = expectedResults.getElements();

		Iterator<DepartmentApi> it = expectedElements.iterator();
		for (DepartmentApi departmentApi : resultElements) {
			DepartmentApi expectedDepartment = it.next();
			AssertUtil.assertDTOEquals(departmentApi, expectedDepartment);
		}
	}
}
